package hk.linkcredit.awardservice.award.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import hk.linkcredit.awardservice.award.domain.vo.ActivityAggregateVo;
import hk.linkcredit.awardservice.award.domain.entity.AwardDistributionRatio;
import hk.linkcredit.awardservice.award.mapper.AwardActivityMapper;
import hk.linkcredit.awardservice.award.service.IAwardActivityService;
import hk.linkcredit.awardservice.award.service.IAwardConfigService;
import hk.linkcredit.awardservice.award.service.IAwardDistributionRatioService;
import hk.linkcredit.awardservice.award.service.IAwardGroupService;
import hk.linkcredit.awardservice.common.constant.ActivityTypeEnum;
import hk.linkcredit.awardservice.award.domain.entity.AwardActivity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 奖励活动Service业务层处理
 *
 * @author african-loan
 * @date 2022-06-24
 */
@Service
@AllArgsConstructor
public class AwardActivityServiceImpl extends ServiceImpl<AwardActivityMapper, AwardActivity> implements IAwardActivityService {


    private IAwardGroupService awardGroupService;

    private IAwardDistributionRatioService distributionRatioService;

    private IAwardConfigService awardConfigService;

    /**
     * 查询奖励活动
     *
     * @param id 奖励活动ID
     * @return 奖励活动
     */
    @Override
    public AwardActivity selectAwardActivityById(Long id) {
        IdWorker.getIdStr();
        return this.getById(id);
    }

    /**
     * 查询奖励活动列表
     *
     * @param awardActivity 奖励活动
     * @return 奖励活动
     */
    @Override
    public List<AwardActivity> selectAwardActivityList(AwardActivity awardActivity) {
        return this.list(new QueryWrapper<>(awardActivity).last("order by id desc"));
    }

    /**
     * 新增奖励活动
     *
     * @param awardActivity 奖励活动
     * @return 结果
     */
    @Override
    public int insertAwardActivity(AwardActivity awardActivity) {
        awardActivity.setCreateTime(new Date());
        return baseMapper.insert(awardActivity);
    }

    /**
     * 修改奖励活动
     *
     * @param awardActivity 奖励活动
     * @return 结果
     */
    @Override
    public int updateAwardActivity(AwardActivity awardActivity) {
        awardActivity.setUpdateTime(new Date());
        return baseMapper.updateById(awardActivity);
    }

    /**
     * 批量删除奖励活动
     *
     * @param ids 需要删除的奖励活动ID
     * @return 结果
     */
    @Override
    public int deleteAwardActivityByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除奖励活动信息
     *
     * @param id 奖励活动ID
     * @return 结果
     */
    @Override
    public int deleteAwardActivityById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public AwardActivity getActivity(ActivityTypeEnum activityTypeEnum) {
        QueryWrapper<AwardActivity> queryWrapper = new QueryWrapper<AwardActivity>().eq("state", "1").eq("act_type", activityTypeEnum.getTypeValue());
        Date now = new Date();
        queryWrapper.le("start_time", now);
        queryWrapper.gt("end_time", now);
        List<AwardActivity> awardActivityList = list(queryWrapper);
        return awardActivityList.size() > 0 ? awardActivityList.get(0) : null;
    }

    @Override
    public ActivityAggregateVo getDistributionActivity() {

        //获取获取信息
        AwardActivity activity = this.getActivity(ActivityTypeEnum.AWARD_DISTRIBUTION);
        if (activity == null) return null;

        ActivityAggregateVo activityAggregateVo = new ActivityAggregateVo();
        activityAggregateVo.setAwardActivity(activity);

        //获取分组信息
        List<String> groups = awardGroupService.getGroups(activity.getId());
        activityAggregateVo.setGroups(groups);

        //获取分销比例
        AwardDistributionRatio distributionRatio = distributionRatioService.getById(activity.getDistributionRatioId());
        activityAggregateVo.setDistributionRatio(distributionRatio);

        //奖励配置
        activityAggregateVo.setAwardConfigs(awardConfigService.getAwardConfigs(activity.getId()));
        return activityAggregateVo;
    }


}
